<?php

session_start();

include_once "src/controller/blmanagement/ReportManagement.php";
include_once "src/controller/blmanagement/ResourceManagement.php";
include_once "src/model/User.php";
include_once "src/misc/ClientReponseGenerator.php";

try {

    $reportResult = ReportManagement::RptResourcesByUsers("2005-01-01", "2020-01-01");

    $values = "";
    $titles = "";
    $colors = "";
    $labels = "";
    $tempUsername = "";
    $tempResourceName = "";

    $reportResultCount = count($reportResult);
    
    for ($i = 0; $i < $reportResultCount; $i++) {
        $tempReportItem = $reportResult[$i];

        if ($tempUsername != $tempReportItem[2]) {

            if ($colors != "") {
                $colors .= ",";
                $titles .= "|";
            }

            $tempUsername = $tempReportItem[2];

            $titles .= $tempReportItem[2];

            mt_srand((double)microtime()*1000000);
            $c = '';
            while(strlen($c)<6) {
                $c .= sprintf("%02X", mt_rand(0, 255));
            }
            $colors .= $c;
        }
    }

    $reportResult = ReportManagement::RptResourcesByUsers2("2005-01-01", "2020-01-01");

    $reportResultCount = count($reportResult);

    for ($i = 0; $i < $reportResultCount; $i++) {
        $tempReportItem = $reportResult[$i];

        if ($tempResourceName != $tempReportItem[0]) {

            $tempResourceName = $tempReportItem[0];

            $labels .= "|".$tempReportItem[0];

            for ($k = $i; $k < $reportResultCount; $k++) {

                $tempReportItem = $reportResult[$k];

                $values .= $tempReportItem[1];

                if (($reportResultCount > 1) && (($k + 1) < $reportResultCount)) {
                    $values .= ",";
                }

                $i++;
                if ($reportResult[$k + 1][0] != $tempResourceName) {
                    $k = $reportResultCount;
                }
            }

            if (($reportResultCount > 1) && (($i + 1) < $reportResultCount)) {
                $values .= "|";
            }

        }
    }


    if ($reportResultCount == 0) {
        throw new Exception();
    } else {
        //http://chart.apis.google.com/chart?cht=bvo&chs=600x400&chd=t:10,50,60,80,40|50,60,100,40,20|30,30,75,20,60&chco=4d89f9,c6d9fd,0000FF&chbh=20&chxt=y&chdl=5|WE|FD&chxl=0:|Jan|Feb|Mar|LO|KU&chxt=x,y
        echo "{\"success\":\"1\", \"report\":\"<img alt='Yellow pie chart' src='http://chart.apis.google.com/chart?cht=bvo&chs=600x400&chd=t:$values&chco=$colors&chbh=20&chxt=y&chdl=$titles&chxl=0:$labels&chxt=x,y&chtt=Resource+Used+Frequency+by+User+Report'>\", \"message\":\"".""."\"}";
    }

} catch (Exception $e) {
    echo ClientReponseGenerator::simpleJsonResponse(0, "GetAllResourcesException", "");
}

?>
